function [ rgbimageFinal ] = fillBriefcase( binaryImage, rgbImage, xyzImage, inimage )
%FILLBRIEFCASE Summary of this function goes here
%   Detailed explanation goes here

maskIm = bwareaopen(binaryImage,50);
%maskIm=bwmorph(maskIm,'dilate',1);
maskIm=bwmorph(maskIm,'dilate',1);
maskIm=bwmorph(maskIm,'erode',1);
maskIm=bwmorph(maskIm,'erode',1);

maskIm = bwareaopen(maskIm,50);

maskIm=bwmorph(maskIm,'dilate',1);
imshow(maskIm);
maskIm = bwlabel(maskIm, 8);
stats = regionprops(maskIm, 'area');
%disp(size(stats));

if (length(stats)>0)
    for i=1:length(stats)
        areas(i)=stats(i).Area;
    end
    %areas = stats.Area
    [maxArea, I] = max(areas); 
    %maskIm = bwareaopen(maskIm,maxArea-2);
else
   I=-1;
end

if I~=-1
    [rows, cols] = find(maskIm == I);

    maskIm = false(480,640);
    points = ones(size(rows(:,1)),3);
    %store the xyz points
    for i=1:size(rows)
      points(i,:)= xyzImage(rows(i),cols(i),:);
    end

    %get the plane coeficients that best fits the points
    coeff = fitplane(points);

    %at each loop 
    for i=1:480
        for j=1:640
            point = xyzImage(i,j,:);
            point = permute(point,[3 1 2]);
            dist(i,j) = getEuclideanDistance(point,coeff);

            if dist(i,j)<0.045 && dist(i,j)~=-1 && rgbImage(i,j,1) < 50 && rgbImage(i,j,2) < 50 && rgbImage(i,j,3) < 50
                %overlay the color from the background image
                maskIm(i,j) = 1;
            end
        end
    end    
end

imshow(maskIm);
maskIm=bwmorph(maskIm,'erode',1);

stats = regionprops(maskIm, 'area');
%disp(size(stats));

if (length(stats)>0)
    for i=1:length(stats)
        areas(i)=stats(i).Area;
    end
    %areas = stats.Area
    [maxArea, I] = max(areas); 
    maskIm = bwareaopen(maskIm,maxArea-2);
end

maskIm=bwmorph(maskIm,'dilate',1);

imshow(maskIm);

[B, L] = bwboundaries(maskIm,'noholes');
boundary = B{1};

eq = zeros(4,2);
%fign = uint8(zeros(480,640));
for i = 1:4
    
    [flag,t,d,nr,nc,count,frl,fcl,newcountl] = ransacline(boundary(:,1),boundary(:,2),2,0.25,0.01,0.001,60,3);
    boundary=setdiff(boundary,[frl fcl],'rows');
    
    %If it lies to the left of the centroid
    eq(i,:) = [sin(t)/cos(t) d/cos(t)];
    
end

%Now we have the corner points. Figure out which
%ones correspond to which corner point
stats = regionprops(maskIm, 'Centroid','BoundingBox');
centroidVal = stats.Centroid;
%centroid = permute(centroid,[2,1]);
centroid = centroidVal([2 1]);
corners = zeros(4,2);
for i=1:4
    for j=1:4
        
       %compute the intersecting point
       A = [eq(i,1) -1; eq(j,1) -1];
       B = [-eq(i,2) -eq(j,2)]';
       point = abs(linsolve(A,B)); 
       
       %if the distance between the intersection point and the 
       %distance = point(1)-centroid(1)
       if norm(point - centroid') < 100
           if (point(1)-centroid(1))*(point(2)-centroid(2)) > 0
              if point(1) < centroid(1)
                  corners(1,:)=point;
              else
                  corners(3,:)=point;
              end
           else
                if point(1) < centroid(1)
                  corners(2,:)=point;
                else
                  corners(4,:)=point;
                end
           end
       end
       
    end 
end
hold on
plot(corners(:,2), corners(:,1),'+');
hold off

%Now find the bounding box for this image
[verticesX, verticesY, centroidBound] = calcBoundingBox(maskIm);
bbWidth = verticesX(2) - verticesX(1);
bbHeight = verticesY(3) - verticesY(1);

%Check for incorrect corner detections. If incorrect, then use the
%bounding box coordinates
if norm(corners(1,:) - centroid)>100
  corners(1,:) = [verticesY(1) verticesX(1)];
end
if norm(corners(2,:) - centroid)>100
   corners(2,:) = [verticesY(1) verticesX(2)];
end
if norm(corners(3,:) - centroid)>100
   corners(3,:) = [verticesY(3) verticesX(2)]; 
end
if norm(corners(4,:) - centroid)>100
   corners(4,:) = [verticesY(3) verticesX(1)]; 
end

%******************************************************************

%Perform an image transfer from the video frame
%to the original image
%*****************************************************************
% get input video frame and its size

%inimage is given as an input parameter
[IR,IC,D]=size(inimage);

%Get the portion of the video to place on the briefcase
UV=zeros(4,2);
XY=zeros(4,2);

%Need to find the target points manually
UV=corners;% target points
    
%The dimensions of the image. 
XY=[[1,1]',[1,IC]',[IR,IC]',[IR,1]']';    % source points

P=esthomog(UV,XY,4);    % estimate homography mapping UV to XY
v=zeros(3,1);
 %at each loop 
    for i=verticesY(1):verticesY(3)
        for j=verticesX(1):verticesX(2)
            
            v=P*[i,j,1]';        % project destination pixel into source
            y=round(v(1)/v(3));  % undo projective scaling and round to nearest integer
            x=round(v(2)/v(3));
            
            %maskIm(i,j)==1 &&
            if (x >= 1) && (x <= IC) && (y >= 1) && (y <= IR)
                %overlay the color from the background image
                rgbImage(i,j,:) = inimage(y,x,:);
            end
        end
    end
    rgbimageFinal = rgbImage;
    
    imshow(rgbimageFinal);
%******************************************************************


end

